
<template>
  <div class="home">
    <header class="home-header" :class="{ active: headerActive }">
      <div class="header-search">
        <svg-icon class="search-icon" icon-class="search"></svg-icon>
        <router-link tag="span" class="search-title" to="./search"
          >推荐搜索 关键词</router-link
        >
      </div>
      <svg-icon
        class="customer-service-icon"
        icon-class="customer-service"
      ></svg-icon>
    </header>
    <van-swipe
      class="swiper-carousel"
      lazy-render
      :autoplay="3000"
      :show-indicators="false"
    >
      <van-swipe-item v-for="(image, index) in homeImgs" :key="index">
        <img class="lazy_img" @click="handleClick" :src="image.imgUrl" />
      </van-swipe-item>
    </van-swipe>

    <section class="home-tags">
      <ul class="tags-content">
        <router-link tag="li" class="tags-item" to="./search">
          <svg-icon
            class="tags-icon"
            icon-class="chain-cat-boutique"
          ></svg-icon>
          <span class="item-text">链猫精品</span>
        </router-link>
        <router-link tag="li" class="tags-item" to="./search">
          <svg-icon class="tags-icon" icon-class="cm-area"></svg-icon>
          <span class="item-text">CM专区</span>
        </router-link>
        <router-link tag="li" class="tags-item" to="./search">
          <svg-icon class="tags-icon" icon-class="collar-cm"></svg-icon>
          <span class="item-text">领CM币</span>
        </router-link>
        <router-link tag="li" class="tags-item" to="./search">
          <svg-icon class="tags-icon" icon-class="coupon-svg"></svg-icon>
          <span class="item-text">领券</span>
        </router-link>
        <router-link tag="li" class="tags-item" to="./search">
          <svg-icon class="tags-icon" icon-class="chain-cat-member"></svg-icon>
          <span class="item-text">链猫会员</span>
        </router-link>
      </ul>
    </section>

    <section class="spike-area">
      <ul class="spike-top">
        <router-link class="top-left" to="/chainCatSpike" tag="li">
          <div class="item-top">
            <span class="item-title">链猫秒杀</span>
            <div class="time-text">
              <span class="eight-time">8点场</span>
              <van-count-down :time="timeData" class="time-count-down">
                <template v-slot="timeData">
                  <span class="time-item" v-if="timeData.hours < 10">{{
                    "0" + timeData.hours
                  }}</span>
                  <span class="time-item" v-else>{{ timeData.hours }}</span>
                  <i class="tow-point">:</i>
                  <span class="time-item" v-if="timeData.minutes < 10">{{
                    "0" + timeData.minutes
                  }}</span>
                  <span class="time-item" v-else>{{ timeData.minutes }}</span>
                  <i class="tow-point">:</i>
                  <span class="time-item" v-if="timeData.seconds < 10">{{
                    "0" + timeData.seconds
                  }}</span>
                  <span class="time-item" v-else>{{ timeData.seconds }}</span>
                </template>
              </van-count-down>
            </div>
          </div>
          <div class="item-info">
            <div class="item-content">
              <img src="../assets/image/home/demo1.png" />
              <span class="new-price">¥298</span>
              <span class="old-price">¥399</span>
            </div>
            <div class="item-content">
              <img src="../assets/image/home/demo2.png" />
              <span class="new-price">¥298</span>
              <span class="old-price">¥399</span>
            </div>
            <div class="item-content">
              <img src="../assets/image/home/demo3.png" />
              <span class="new-price">¥298</span>
              <span class="old-price">¥399</span>
            </div>
          </div>
        </router-link>
        <router-link class="top-right" to="/foundGoodGoods" tag="li">
          <div class="right-header">
            <span class="cat-spike-text">发现好货</span>
            <span class="tag-text">品质好物</span>
          </div>
          <span class="good-item">好物不贵</span>
          <div class="item-imgs">
            <img src="../assets/image/home/demo4.png" />
            <img src="../assets/image/home/demo5.png" />
          </div>
        </router-link>
      </ul>

      <ul class="spike-center">
        <router-link class="center-item" to="/specialSpike" tag="li">
          <span class="center-title">特价秒杀</span>
          <span class="center-descr">10元抢购</span>
          <img src="../assets/image/home/demo6.png" />
        </router-link>
        <router-link class="center-item" to="/brandSpike" tag="li">
          <span class="center-title">品牌秒杀</span>
          <span class="center-descr" style="color: #dd3749">笔记本秒杀</span>
          <img src="../assets/image/home/demo7.png" />
        </router-link>
        <router-link class="center-item" to="/newProductLaunch" tag="li">
          <span class="center-title">新品首发</span>
          <span class="center-descr" style="#FC6380">小黑盒新品</span>
          <img src="../assets/image/home/demo8.png" />
        </router-link>
        <router-link class="center-item" to="/premiumRanking" tag="li">
          <span class="center-title">优品排行</span>
          <span class="center-descr" style="color: #91c95b">榜上好物购</span>
          <img src="../assets/image/home/demo9.png" />
        </router-link>
      </ul>
      <ul class="spike-bottom">
        <li class="bottom-left">
          <div class="bottom-left-header">
            <span class="big-buy">大牌购走</span>
            <span class="goods-name">
              戴尔
              <svg-icon icon-class="right-arrow"></svg-icon>
            </span>
          </div>
          <span class="belive-big">信赖大品牌</span>
          <div class="bottom-images">
            <img src="../assets/image/home/demo10.png" />
            <img src="../assets/image/home/demo11.png" />
          </div>
        </li>
        <router-link class="bottom-left" to="/loveShop" tag="li">
          <div class="bottom-left-header">
            <span class="big-buy">爱逛好店</span>
            <span class="goods-name addColor">懂你所要</span>
          </div>
          <span class="belive-big">来逛个够</span>
          <div class="bottom-images">
            <img src="../assets/image/home/demo12.png" />
            <img src="../assets/image/home/demo13.png" />
          </div>
        </router-link>
      </ul>
    </section>

    <div class="content-tabs">
      <van-tabs
        :swipe-threshold="5"
        title-inactive-color="#3a3a3a"
        title-active-color="#D8182D"
        background="transparent"
        v-model="active"
        animated
      >
        <van-tab
          v-for="(list, index) in tabArray"
          :title="list.describe"
          :name="list.type"
          :key="index"
        >
          <template #title>
            <div class="slot-title">
              <b class="tab-title">{{ list.title }}</b>
              <!-- <span class="tab-name">{{list.describe}}</span> -->
              <span class="tab-name">{{ list.name }}</span>
            </div>
          </template>

          <section class="goods-box search-wrap">
            <ul class="goods-content">
              <li v-for="(item, index) in list.list" :key="index">
                <router-link class="goods-img" tag="div" to="/classify/product">
                  <img :src="item.img" />
                </router-link>
                <div class="goods-layout">
                  <div class="goods-title">{{ item.productName }}</div>
                  <span class="goods-div">{{ item.title }}</span>
                  <div class="goods-desc">
                    <span class="goods-price">
                      <i>{{ item.productCnyPrice }}</i>
                      <span class="force-value"
                        >{{ item.forceValue }}倍算力</span
                      >
                    </span>
                    <span class="add-icon" @click="addToCart($event, item)">
                      <svg-icon icon-class="add"></svg-icon>
                    </span>
                  </div>
                </div>
              </li>
            </ul>
          </section>
        </van-tab>
      </van-tabs>
    </div>
    <div class="ballWrap">
      <transition
        @before-enter="beforeEnter"
        @enter="enter"
        @afterEnter="afterEnter"
      >
        <div class="ball" v-if="show">
          <li class="inner">
            <span class="cubeic-add" @click="addToCart($event, item)">
              <svg-icon class="add-icon" icon-class="add"></svg-icon>
            </span>
          </li>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted, toRefs, getCurrentInstance } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import Tabbar from "@/components/Tabbar";
export default {
  name: "home",
  components: {
    Tabbar,
  },
  setup(props, context) {
    const { ctx } = getCurrentInstance();
    const $store = useStore();

    const $router = useRouter();
    const $route = useRoute(); // 可以拿到所有和路由相关的参数
    const active = ref("");
    const timeData = ref(36000000);
    const headerActive = ref(false);

    const state = reactive({
      homeImgs: [],
      tabArray: [],
    });

    const ball = reactive({
      show: false,
      el: "",
    });

    ctx.$http.get("http://test.happymmall.com/home/homeData").then((res) => {
      const { images, tabList } = res.data;
      state.tabArray = tabList;
      state.homeImgs = images;
    });

    onMounted(() => {
      // ctx.$eventBus.$emit("changeTag", 0);
      window.addEventListener("scroll", pageScroll);
    });

    const addToCart = (event, tag) => {
      // console.log(event, tag);
      $store.commit("addToCart", tag);
      ball.show = true;
      ball.el = event.target;
    };

    const beforeEnter = (el) => {
      const dom = ball.el;
      const rect = dom.getBoundingClientRect();
      const x = rect.left - window.innerWidth * 0.6;
      const y = -(window.innerHeight - rect.top);
      el.style.display = "block";
      el.style.transform = `translate3d(0,${y}px,0)`;
      const inner = el.querySelector(".inner");
      inner.style.transform = `translate3d(${x}px,0,0)`;
    };

    const enter = (el, done) => {
      document.body.offsetHeight;
      el.style.transform = "translate3d(0,0,0)";
      const inner = el.querySelector(".inner");
      inner.style.transform = "translate3d(0,0,0)";
      el.addEventListener("transitionend", done);
    };

    const afterEnter = (el) => {
      ball.show = false;
      el.style.display = "none";
    };

    const handleClick = (linkUrl) => {
      $router.push("/classify/product");
    };

    const pageScroll = () => {
      const scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop > 100
        ? (headerActive.value = true)
        : (headerActive.value = false);
    };

    return {
      active,
      timeData,
      headerActive,
      ...toRefs(ball),
      ...toRefs(state),
      addToCart,
      beforeEnter,
      enter,
      afterEnter,
      handleClick,
      pageScroll,
    };
  },
};
</script>

<style scoped lang="scss">
@import "../styles/mixin.scss";
.home {
  .home-header {
    &.active {
      display: none;
    }
    position: fixed;
    left: 0;
    top: 10px;
    width: 100%;
    height: 40px;
    line-height: 40px;
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    @include boxSizing;
    font-size: 30px;
    color: #fdc;
    z-index: 10000;
    .header-search {
      border-radius: 3px;
      display: flex;
      width: 90%;
      height: 40px;
      line-height: 40px;
      color: #232326;
      background: #fff;
      .search-icon {
        line-height: 40px;
        padding: 8px 10px;
      }
      .app-name {
        padding: 0 20px;
        color: $red;
        font-size: 40px;
        font-weight: bold;
      }
      .icon-search {
        padding: 0 20px;
        font-size: 34px;
      }
      .search-title {
        font-size: 16px;
        color: #dbdbdb;
        width: 100%;
        padding-left: 80px;
      }
    }
  }

  .swiper-carousel {
    padding-bottom: 10px;
    .lazy_img {
      width: 100%;
      height: 250px;
    }
  }
  .swiper-cls {
    background-color: #efeff4;
    .swiper-slide {
      padding-left: 16px;
      background-color: #efeff4;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .slide_img {
      width: 222px;
      height: 90px;
      border-radius: 5px;
    }
  }
  .home-tags {
    margin-top: 23px;
    .tags-content {
      display: flex;
      justify-content: space-around;
      align-items: center;
      .tags-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .tags-icon {
          width: 43px;
          height: 43px;
        }
        .item-text {
          padding-top: 5px;
          font-size: 11px;
          color: #3a3a3a;
        }
      }
    }
  }
  .spike-area {
    margin: 12px;
    padding-top: 5px;
    border-radius: 4px;
    background-color: white;
    .spike-top {
      display: flex;
      justify-content: space-between;
      align-items: stretch;
      .top-left {
        padding: 12px;
        .item-top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .item-title {
            font-size: 14px;
            color: #3a3a3a;
            padding-right: 5px;
            font-weight: 600;
          }
          .time-text {
            border: 1px solid #ccc;
            width: 110px;
            height: 18px;
            line-height: 18px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .eight-time {
              font-size: 11px;
              color: white;
              text-align: center;
              width: 42px;
              background-color: #d8182d;
            }
            .time-count-down {
              flex: 1;
              font-size: 9px;
              color: #dd3749;
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
        }
        .item-info {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .item-content {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding-top: 4px;
            .new-price {
              font-size: 11px;
              color: #d8182d;
              font-weight: 600;
            }
            .old-price {
              font-size: 9px;
              color: #949497;
              text-decoration: line-through;
            }
          }
        }
      }
      .top-right {
        background-color: white;
        font-size: 11px;
        padding: 12px;
        border-radius: 4px;
        flex: 1;
        .right-header {
          .cat-spike-text {
            font-size: 14px;
            color: #3a3a3a;
            font-weight: 600;
          }
          .tag-text {
            border: 1px solid #d8182d;
            display: inline-block;
            text-align: center;
            margin-left: 7px;
            border-radius: 2px;
            color: #d8182d;
            width: 64px;
          }
        }
        .good-item {
          color: #949497;
          font-size: 11px;
        }
        .item-imgs {
          display: flex;
          justify-content: space-between;
          align-items: center;
          img {
            width: 63px;
            height: 63px;
          }
        }
      }
    }
    .spike-center {
      padding-top: 20px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .center-item {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .center-title {
          font-size: 14px;
          color: #3a3a3a;
          font-weight: 600;
        }
        .center-descr {
          font-size: 11px;
          color: #ff9351;
        }
        img {
          width: 70px;
          height: 70px;
        }
      }
    }
    .spike-bottom {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .bottom-left {
        padding: 12px;
        display: flex;
        flex-direction: column;

        .bottom-left-header {
          .big-buy {
            font-size: 14px;
            color: #3a3a3a;
            font-weight: 600;
          }
          .goods-name {
            width: 66px;
            height: 18px;
            display: inline-block;
            border-radius: 2px;
            margin-left: 8px;
            text-align: center;
            border: 1px solid #d8182d;
            font-size: 11px;
            color: #dd3749;
            .svg-icon {
              width: 4px;
              height: 7px;
            }
          }
          .addColor {
            color: #dbbaff;
            border: 1px solid #dbbaff;
          }
        }
        .belive-big {
          font-size: 11px;
          color: #949497;
        }
        .bottom-images {
          display: flex;
          justify-content: flex-start;
          align-items: center;
        }
      }
    }
  }
  .content-tabs {
    padding-top: 10px;
    padding-bottom: 50px;
    .slot-title {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      .tab-title {
        font-size: 14px;
        font-weight: 600;
        font-size: 17px;
        height: 24px;
        display: inline-block;
        width: 40px;
        line-height: 22px;
      }
      .tab-name {
        line-height: 16px;
        font-size: 10px;
        display: inline-block;
      }
    }
    .goods-box {
      padding: 10px 16px;
      .good-things {
        font-size: 18px;
        color: #d8182d;
        font-weight: 600;
      }
      .goods-content {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        li {
          display: inline-block;
          width: 165px;
          margin-top: 10px;
          border-radius: 8px;
          background-color: white;
          box-shadow: 0 5px 15px 0 rgba(0, 0, 0, 0.1);
          img {
            width: 100%;
          }
        }
        li:nth-of-type(even) {
          padding-right: 0;
        }
        .goods-img {
          img {
            width: 165px;
            height: 196px;
            border-radius: 8px 8px 0 0;
          }
        }
        .goods-layout {
          width: 165px;
          padding: 0 10px;
          display: flex;
          justify-content: flex-start;
          flex-direction: column;
          .goods-title {
            color: #3a3a3a;
            font-size: 14px;
            font-weight: 600;
          }
          .goods-div {
            color: #949497;
            font-size: 11px;
          }
          .goods-desc {
            background-color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 10px;
            .goods-price {
              padding-top: 5px;
              font-size: 14px;
              color: #d8182d;
              display: flex;
              justify-content: center;
              align-items: center;
              .force-value {
                margin-left: 7px;
                color: white;
                border-radius: 20px 20px;
                background-color: #d8182d;
                display: inline-block;
                font-size: 7px;
                line-height: 17px;
                text-align: center;
                width: 42px;
                height: 17px;
              }
            }
            .add-icon {
              display: flex;
              justify-content: center;
              align-items: center;
            }
          }
        }
      }
    }
  }

  .ballWrap {
    .ball {
      position: fixed;
      left: 60%;
      bottom: 10px;
      z-index: 1003;
      color: red;
      transition: all 0.5s cubic-bezier(0.49, -0.29, 0.75, 0.41);
      .inner {
        width: 16px;
        height: 16px;
        transition: all 0.5s linear;
      }
    }
  }
}
</style>
